﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace YTCMS.Core
{
    public class EmailSender
    {
        private SmtpClient smtp;
        public string SmtpServer
        {
            get;
            set;
        }
        public int SmtpPort
        {
            get;
            set;
        }
        public bool EnableSsl
        {
            get;
            set;
        }
        public string Username
        {
            get;
            set;
        }
        public string Password
        {
            get;
            set;
        }
        public string ReplyAddress
        {
            get;
            set;
        }
        public string DisplayName
        {
            get;
            set;
        }
        protected bool NeedProtect
        {
            get
            {
                return true;
            }
        }
        public string Description
        {
            get
            {
                return string.Empty;
            }
        }
        public string Logo
        {
            get
            {
                return string.Empty;
            }
        }
        public string ShortDescription
        {
            get
            {
                return string.Empty;
            }
        }

        public EmailSender()
        {
            this.SmtpServer = "smtp.126.com";
            this.DisplayName = "WZT";
            this.EnableSsl = false;
            this.Password = "wopeng870312";
            this.ReplyAddress = "wopeng@126.com";
            this.Username = "wopeng@126.com";
            this.SmtpPort = 25;
            this.smtp = new SmtpClient(this.SmtpServer, this.SmtpPort)
            {
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(this.Username, this.Password),
                EnableSsl = this.EnableSsl,
                DeliveryMethod = SmtpDeliveryMethod.Network
            };
        }
        public bool Send(MailMessage mail, Encoding emailEncoding)
        {
            if (mail == null)
            {
                throw new ArgumentNullException("mail");
            }
            if (mail.From == null)
            {
                mail.From = new MailAddress(this.ReplyAddress, this.DisplayName, emailEncoding);
            }
            if (mail.IsBodyHtml)
            {
                mail.Body = mail.Body;
            }
            mail.BodyEncoding = emailEncoding;
            this.smtp.Send(mail);
            return true;
        }
    }
}
